Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inherit all Enums from str to make JSON serialization possible #132

Merged
merged 1 commit into from
Jul 16, 2024

Conversation

adubovik
Copy link
Collaborator

@adubovik adubovik commented Jul 12, 2024

The following code throws an exception, because Enums aren't JSON serializable.
One needs to inherit Enum from str to make it work.

import json
from aidial_sdk.chat_completion import Message, Role

msg = Message(role=Role.SYSTEM, content="test")
dict = msg.dict(exclude_none=True)
print(json.dumps(dict)) # exception: TypeError: Object of type Role is not JSON serializable

The pattern of converting DIAL messages to OpenAI message (which is Python dict) is common in DIAL applications.
The exception doesn't allow to do it in a straigh-forward way and one have to resort to the hack like this:

import json
from aidial_sdk.chat_completion import Message, Role

msg = Message(role=Role.SYSTEM, content="test")
dict = msg.dict(exclude_none=True)
+ dict["role"] = dict["role"].value
print(json.dumps(dict)) # ok

The PR allows to avoid this hack.

@adubovik adubovik self-assigned this Jul 12, 2024
@adubovik adubovik force-pushed the feat/inherit-enums-from-str branch from a8aafb9 to 4293e64 Compare July 16, 2024 14:37
@adubovik adubovik merged commit 2aca1ac into development Jul 16, 2024
12 checks passed
@adubovik adubovik deleted the feat/inherit-enums-from-str branch July 16, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants